import { hrmCertificateApi } from "@/api/HRMS/Certificate/index";
import { thStaffsGetStaffsList } from "@/api/system/user";
import { BasicDrawer, useDrawerInner } from "@/components/Drawer";
import { BasicForm, FormSchema, useForm } from "@/components/Form";
import { message } from "antd";
import { useTranslation } from "react-i18next";
let reload: Function;
let certId: string;
let staffId: string;
const timeFn = (time: string) => {
	if (!time) {
		return "";
	}
	const [date] = time.split(" ");
	return date;
};
const index = (props: { register: Fn }) => {
	const { t } = useTranslation();
	const ev_vi = "HRMS.certificate";
	const formSchema: FormSchema[] = [
		{
			label: t(`${ev_vi}.staffName`),
			field: "staffName", //姓名
			component: "ApiSelect",
			componentProps: {
				api: thStaffsGetStaffsList,
				labelField: "staffName",
				valueField: "staffName",
				onChange: (_: any, value: any) => {
					if (!value) return;
					staffId = value.staffId;
				}
			}
		},
		{
			label: t(`${ev_vi}.certificate`),
			field: "certName", //证书名称
			component: "Input"
		},
		{
			label: t(`${ev_vi}.certificateType`),
			field: "certType", //证书类型
			component: "Input"
		},
		{
			label: t(`${ev_vi}.certificateNo`),
			field: "certNumb", //证书编号
			component: "Input"
		},
		{
			label: t(`${ev_vi}.issuingAuthority`),
			field: "issueAuthority", //发行机关
			component: "Input"
		},
		{
			label: t(`${ev_vi}.certificateLevel`),
			field: "certLevel", //证书等级
			component: "Input"
		},
		{
			label: t(`${ev_vi}.dateOfIssue`),
			field: "startDate", //发证日期
			component: "DatePicker"
		},
		{
			label: t(`${ev_vi}.effectiveDate`),
			field: "endDate", //有效日期
			component: "DatePicker"
		},
		{
			label: t(`${ev_vi}.effectiveDate`),
			field: "certUrl", //有效日期
			component: "FileUpload",
			componentProps: {
				maxCount: 1,
				fileType: "image"
			}
		}
	];
	const [registerDrawer, { closeDrawer }] = useDrawerInner(props.register, data => {
		reload = data.reload;
		resetFields();
		if (data.record) {
			console.log(data.record);

			certId = data.record.certId;
			staffId = data.record.staffId;
			console.log(staffId);

			setFieldsValue(data.record);
		}
	});

	const [register, { validateFields, setFieldsValue, resetFields }] = useForm({
		schemas: formSchema,
		labelWidth: 100,
		baseColProps: {
			span: 24
		}
	});
	//提交
	const handleSubmit = async () => {
		let formValue = await validateFields();
		let api = hrmCertificateApi.hrmCertsCreate;
		formValue = {
			...formValue,
			staffId
		};
		if (certId) {
			api = hrmCertificateApi.hrmCertsUpdate;
		}
		formValue.startDate = timeFn(formValue.startDate);
		formValue.endDate = timeFn(formValue.endDate);
		const res = await api({ ...formValue, certId });
		if (res) {
			certId ? message.success(t("kevin_ui.updateSuccess")) : message.success(t("kevin_ui.addedSuccessfully"));
			closeDrawer();
			reload();
		}
	};

	return (
		<BasicDrawer showFooter={true} {...props} register={registerDrawer} ok={handleSubmit}>
			<div>
				<BasicForm register={register}></BasicForm>
			</div>
		</BasicDrawer>
	);
};
export default index;
